<?php
/**
 * LoginStore.php
 * 描述
 * Created by Paco
 * Date: 2018/8/15
 * Time: 22:36
 */

namespace App\Http\Request\Auth;


use App\Http\Request\Store;
use App\Models\Auth\Admin;
use Illuminate\Support\Facades\Cache;

class LoginStore extends Store
{

    public function rules()
    {
        return [
            'username' => 'required|regex:/^1[345678]\d{9}$/',
            'password' => 'required|min:8'
        ];
    }

    public function attributes()
    {
        return [
            'username' => '手机号',
            'password' => '密码'
        ];
    }

    public function after()
    {
        //如果密码错误超过3次，切换置手机验证码登录
        $params = $this->validateData();
        $key = config('cache.admin.login') . 'failed_' . $params['username'];
        if (app('phpredis')->get($key) > 2) {
            if (empty($params['uuid'])) {
                return $this->throwRespondError(10305);
            } elseif (empty($params['code'])) {
                return $this->throwRespondError(10304);
            } elseif (!check_code($params['uuid'], $params['code'])) {
                return $this->throwRespondError(10302);
            }
        }

        $admin = Admin::query()->where('tel', $params['username'])->count();
        //用户不存在则返回
        if (!$admin) {
            return $this->respondError(10300);
        }

        return true;
    }

}